package cn.tianyun.pethome.org.controller;


import cn.tianyun.pethome.basic.util.AjaxResult;
import cn.tianyun.pethome.basic.util.PageList;
import cn.tianyun.pethome.org.domain.Type;
import cn.tianyun.pethome.org.query.DepartmentQuery;
import cn.tianyun.pethome.org.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private ITypeService typeService;

    @PostMapping
    public PageList<Type> loadAll(@RequestBody DepartmentQuery departmentQuery){
        return  typeService.loadAll(departmentQuery);
    }
    @PutMapping
    public AjaxResult save(@RequestBody Type type){
        System.out.println(type);
        try {
            if(type.getId()==null){
                typeService.add(type) ;
                return AjaxResult.creat().setSuccess(true).setMsg("添加成功");
            }else {
                typeService.update(type);
                return AjaxResult.creat().setSuccess(true).setMsg("修改成功");
            }

        }catch (Exception e ){
            return AjaxResult.creat().setSuccess(false).setMsg("失败");
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            typeService.delete(id);
            return AjaxResult.creat().setSuccess(true).setMsg("删除成功");
        }catch (Exception e){
            return AjaxResult.creat().setSuccess(false).setMsg("删除失败");
        }

    }
}
